package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneFuncProdInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.ProdInstDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.dto.ContextDTO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.staticdata.ServiceOfferConst;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SafeCheckNumCantLessAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        String serviceOfferId = data.getServiceOfferId();
        //String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode");
        //if(KiteStringUtils.equals(serviceOfferId, ServiceOfferConst.service_offer_2826)&&!KiteStringUtils.equals(nodeCode,"OrderResubmit")){
        if(KiteStringUtils.equals(serviceOfferId, ServiceOfferConst.service_offer_2826)){
            if(KiteStringUtils.isNotEmpty(attr.getOriginValue())){//如果有初始值 与新值比较
                int originValue = StrToNum(attr.getOriginValue());
                int valueNum = StrToNum(value);
                if(valueNum<originValue){
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("变更次数要大于等于订购次数！");
                }
            }
        }
        super.set(attr, inst, value, data);
    }

    @Override
    public void init(KiteAttr attr, KiteInst inst) {
        super.init(attr, inst);
    }

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        super.onShow(attr, inst, data);
    }
    private int StrToNum(String numStr){
        if(KiteStringUtils.isEmpty(numStr)){
            return 0;
        }
        if(KiteStringUtils.isNum(numStr)){
            return Integer.parseInt(numStr);
        }
        return 0;
    }

}
